package com.bootx.dao.impl;

import com.bootx.dao.RedPackageDao;
import com.bootx.entity.RedPackage;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author black
 */
@Repository
public class RedPackageDaoImpl extends BaseDaoImpl<RedPackage,Long> implements RedPackageDao {
    @Override
    public List<RedPackage> getRedPackageByFolderId(Long batchId) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<RedPackage> criteriaQuery = criteriaBuilder.createQuery(RedPackage.class);
        Root<RedPackage> root = criteriaQuery.from(RedPackage.class);
        Predicate restrictions = criteriaBuilder.conjunction();
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("batchId"), batchId));
        criteriaQuery.where(restrictions);
        criteriaQuery.orderBy(criteriaBuilder.asc(root.get("fullTime")),criteriaBuilder.asc(root.get("order")));
        return super.findList(criteriaQuery);
    }

    @Override
    public List<RedPackage> getRedPackageByFolderIdAndTime(Long batchId, String time) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<RedPackage> criteriaQuery = criteriaBuilder.createQuery(RedPackage.class);
        Root<RedPackage> root = criteriaQuery.from(RedPackage.class);
        Predicate restrictions = criteriaBuilder.conjunction();
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("batchId"), batchId));
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("time"), time));
        criteriaQuery.where(restrictions);
        criteriaQuery.orderBy(criteriaBuilder.asc(root.get("fullTime")),criteriaBuilder.asc(root.get("order")));
        return super.findList(criteriaQuery);
    }
}
